<style>
    .layui-table-cell {
        height: 36px;
    }

    .layui-table td img {
        height: 36px;
    }

    .font1 {
        font-weight: bold;
        color: #2d8db1;
        text-decoration: underline;
        cursor: pointer;
    }
</style>

<script type="text/html" template>
    <title>{{layui.setter.MenuName}}-{{layui.setter.name}}</title>
</script>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <script type="text/html" template>
            <a><cite>{{layui.setter.MenuName}}</cite></a>
        </script>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">关键字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="key" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class='layui-form-label'>是否公开 </label>
                    <div class='layui-input-block'>
                        <div class='layui-input-inline'>

                            <select name='IsOpen_Sel' id='IsOpen_Sel' lay-filter='IsOpen_Sel'>
                                <option value='-1' {{ d.params.Type_Sel == '-1' ? 'selected' : '' }}>请选择</option>
                                <option value='0' {{ d.params.Type_Sel == '0' ? 'selected' : '' }}>公开</option>
                                <option value='1' {{ d.params.Type_Sel == '1' ? 'selected' : '' }}>不公开</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class='layui-form-label'>使用条件 </label>
                    <div class='layui-input-block'>
                        <div class='layui-input-inline'>
                            <select name='Use_Sel'>
                                <option value='-1' {{ d.params.Use_Sel == '-1' ? 'selected' : '' }}>请选择</option>
                                <option value='0' {{ d.params.Use_Sel == '0' ? 'selected' : '' }}>满减</option>
                                <option value='1' {{ d.params.Use_Sel == '1' ? 'selected' : '' }}>无门槛</option>
                            </select>
                        </div>
                    </div>
                </div>


            </div>


            <div class="layui-form-item">

                <div class="layui-inline">
                    <label class='layui-form-label'>优惠卷状态 </label>
                    <div class='layui-input-block'>
                        <div class='layui-input-inline'>
                            <select name='State_Sel'>
                                <option value='-1' {{ d.params.State_Sel == '-1' ? 'selected' : '' }}>请选择</option>
                                <option value='0' {{ d.params.State_Sel == '0' ? 'selected' : '' }}>待激活</option>
                                <option value='1' {{ d.params.State_Sel == '1' ? 'selected' : '' }}>激活</option>
                                <option value='2' {{ d.params.State_Sel == '2' ? 'selected' : '' }}>冻结</option>
                                <option value='3' {{ d.params.State_Sel == '3' ? 'selected' : '' }}>回收</option>
                            </select>
                        </div>
                    </div>
                </div>


                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>

        </div>



        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <script type="text/html" template lay-url="{{ layui.setter.APIUrl }}/shop/couponMgr/GetButtonRight"
                    lay-type="post" lay-done="layui.data.setActive()">
                    {{ d.Data }}
                </script>
            </div>
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>
    </div>
</div>
<script>


    var checkIDs = []; //选中的ID
    var checkIDs1 = []; //选中的ID
    var storeName = "";//选中的店铺名称

    layui.use(['admin', 'table', 'form'], function () {

        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form;

        form.render(null, 'app-content-list');
        form.render('select');

        table.render({
            elem: '#LAY-app-content-list'
            , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , defaultToolbar: ['filter']
            , method: "post"
            , url: layui.setter.APIUrl + '/shop/couponMgr/newlistPain'
            , headers: { //通过 request 头传递
                Authorization: "bearer " + layui.data('layuiAdmin').access_token
            }
            , parseData: function (res) { //res 即为原始返回的数据 
                varButtonRight = res.Data.ButtonRight;
                return {
                    "ResultCode": res.ResultCode, //解析接口状态
                    "ResultInfo": res.ResultInfo, //解析提示文本
                    "count": res.Data.TotalRecordCount, //解析数据长度
                    "Data": res.Data.Items //解析数据列表 
                };
            }
            , cols: [[
                { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100
                { field: 'Code', title: '优惠卷编码', width: 160 },
                {
                    field: 'Type_Sel', title: '优惠卷类型 ', templet: function (d) {
                        var Type_Sel = d.Type_Sel;
                        var strDes = '优惠卷类型 [0.商品优惠卷|1.店铺优惠卷|2.平台优惠卷|3.新人优惠券]';
                        var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
                        var lists = strDes.split('|');
                        for (var i = 0; i < lists.length; i++) {
                            if (lists[i].split('.')[0] == Type_Sel) {
                                return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                            }
                        }
                    }, minWidth: 80, align: 'center'
                },
                // { field: 'StoreSource', title: '指定店铺'},                         
                // { field: 'GoodSource', title: '指定商品'},                         
                { field: 'Title', title: '优惠卷标题' },
                {
                    field: 'Use_Sel', title: '使用条件 ', templet: function (d) {
                        var Use_Sel = d.Use_Sel;
                        var strDes = '使用条件 [0.满减|1.无门槛]';
                        var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
                        var lists = strDes.split('|');
                        if (lists[0].split('.')[0] == Use_Sel) {
                            return '<button class="layui-btn layui-btn-xs">' + lists[0].split('.')[1] + "</button>";// layui-btn-primary
                        }
                        else {
                            return '<button class="layui-btn layui-btn-primary layui-btn-xs">' + lists[1].split('.')[1] + "</button>";// layui-btn-primary
                        }
                    }, width: 90, align: 'center'
                },
                { field: 'SillPrice', title: '门槛金额', width: 90 },
                { field: 'Price', title: '优惠卷面额', width: 100 },
                // { field: 'MemberLevelSource', title: '会员等级限制'},                         
                { field: 'GrantNum', title: '发放数量', width: 90 },
                { field: 'GrantStartTime', title: '发放开始时间', templet: '<div>{{ Format(d.GrantStartTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },
                { field: 'GrantEndTime', title: '发放结束时间', templet: '<div>{{ Format(d.GrantEndTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },
                { field: 'CouponCode', title: '新人优惠券组', width: 90 },
                { field: 'LimitNum', title: '限领数量', width: 90 },
                {
                    field: 'IsOpen_Sel', title: '是否公开 ', templet: function (d) {
                        var IsOpen_Sel = d.IsOpen_Sel;
                        var strDes = '是否公开 [0.公开|1.不公开]';
                        var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
                        var lists = strDes.split('|');
                        if (lists[0].split('.')[0] == IsOpen_Sel) {
                            return '<button class="layui-btn layui-btn-xs">' + lists[0].split('.')[1] + "</button>";// layui-btn-primary
                        }
                        else {
                            return '<button class="layui-btn layui-btn-primary layui-btn-xs">' + lists[1].split('.')[1] + "</button>";// layui-btn-primary
                        }
                    }, width: 90, align: 'center'
                },
                // { field: 'UseStartTime', title: '生效时间' ,templet: '<div>{{ Format(d.UseStartTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },                         
                // { field: 'UseEndTime', title: '截止时间' ,templet: '<div>{{ Format(d.UseEndTime,"yyyy-MM-dd hh:mm:ss") }}</div>' },                         
                //{ field: 'ApplyNum', title: '已领取数量'},                         
                {
                    field: 'State_Sel', title: '优惠卷状态 ', templet: function (d) {
                        var State_Sel = d.State_Sel;
                        var strDes = '优惠卷状态 [0.待激活|1.激活|2.冻结|3.回收]';
                        var strDes = strDes.substr(strDes.indexOf("[") + 1, strDes.indexOf("]") - strDes.indexOf("[") - 1);
                        var lists = strDes.split('|');
                        for (var i = 0; i < lists.length; i++) {
                            if (lists[i].split('.')[0] == State_Sel) {
                                return '<button class="layui-btn layui-btn-xs">' + lists[i].split('.')[1] + "</button>";// layui-btn-primary
                            }
                        }
                    }, width: 100, align: 'center'
                },
                { field: 'Description', title: '优惠卷描述' },
                //{ field: 'RMK', title: '备注'},                         
                {
                    field: 'QRCode', title: '二维码地址', templet: function (d) {
                        if (d.QRCode == "" || d.QRCode == null) { return ""; }
                        var html = "<img src=" + layui.setter.ImgPath + d.QRCode + " alt='' />";
                        return html;
                    }
                },
                {
                    title: '操作', minWidth: 165, fixed: 'right', align: 'center', templet: function (d) {
                        var strRtn = "";
                        if (varButtonRight.indexOf('view') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" lay-event=\"view\">查看</a> ";
                        }
                        if (varButtonRight.indexOf('edit') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
                        }
                        if (varButtonRight.indexOf('del') >= 0) {
                            strRtn += "<a class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\">删除</a> ";
                        }
                        return strRtn;
                    }
                }
            ]]
            , page: true
            , limit: 10
            , limits: [10, 15, 20, 25, 30]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
            }
        });

        //监听工具条
        table.on('tool(LAY-app-content-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    admin.req({
                        url: layui.setter.APIUrl + '/shop/couponMgr/BatchDelete' //实际使用请改成服务端真实接口
                        , data: { "ids": data.PID }
                        , type: "post"
                        //, success: function (res) {
                        //}
                        , done: function (res) {
                            layer.close(index); //执行关闭 
                            table.reload('LAY-app-content-list');
                            layer.msg('删除成功');
                        }
                    });
                });
            }
            else if (obj.event === 'edit') {
                //location.hash = '/shop/shop_coupon/addChild/id=' + data.PID;
                admin.popup({
                    title: '编辑信息'
                    , area: ['850px', '650px']
                    , zIndex: 1003
                    , id: 'LAY-popup-content-edit'
                    , success: function (layero, index) {
                        view(this.id).render('/shop/shop_coupon/newcoupon', data).done(function () {
                            form.render(null, 'layuiadmin-app-form-list');


                            //监听提交
                            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                var field = data.field; //获取提交的字段 
                                //提交 Ajax 成功后，关闭当前弹层并重载表格 


    field.Type_Sel =3;

                                admin.req({
                                    url: layui.setter.APIUrl + '/shop/couponMgr/update' //实际使用请改成服务端真实接口
                                    , data: field
                                    , type: "post"
                                    , done: function (res) {
                                        layui.table.reload('LAY-app-content-list'); //重载表格
                                        layer.close(index); //执行关闭
                                        layer.msg('修改成功');
                                    }
                                });
                            });
                        });
                    }
                });
            }
            else if (obj.event === 'view') {
                admin.popup({
                    title: '查看信息'
                    , area: ['850px', '650px']
                    , id: 'LAY-popup-content-view'
                    , success: function (layero, index) {
                        view(this.id).render('/shop/shop_coupon/view', data).done(function () {
                            //监听提交
                            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            }
        });



        form.render('select');



        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
    });

    layui.data.setActive = function () {

        layui.use(['form', 'admin', 'table'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var view = layui.view;
            var table = layui.table;
            var active = {
                del: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    //return layer.msg('已删除');
                    layer.confirm('确定删除吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/couponMgr/BatchDelete' //实际使用请改成服务端真实接口
                            , data: { "ids": ids }
                            , type: "post"
                            //, success: function (res) {
                            //}
                            , done: function (res) {
                                layer.close(index); //执行关闭
                                layer.msg(res.ResultInfo);
                                table.reload('LAY-app-content-list');
                            }
                        });
                    });
                },
                //添加
                add: function (othis) {
                    //location.hash = '/shop/shop_coupon/addChild';           
                    admin.popup({
                        title: '添加信息'
                        , skin: 'layui-layer-lan'
                        , area: ['60%', '85%']
                        , zIndex: 1003
                        , id: 'LAY-popup-content-add'
                        , success: function (layero, index) {
                            view(this.id).render('/shop/shop_coupon/newcoupon').done(function () {
                                form.render(null, 'layuiadmin-app-form-list');

                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                    var field = data.field; //获取提交的字段
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    //请求登入接口
                                    
                                    field.Type_Sel =3;

                                    admin.req({
                                        url: layui.setter.APIUrl + '/shop/couponMgr/Add' //实际使用请改成服务端真实接口
                                        , data: field
                                        , type: "post"
                                        , done: function (res) {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-app-content-list'); //重载表格
                                            layer.msg('新增成功');
                                        }
                                    });
                                });
                            });
                        }
                    });
                },

                sendCoupon: function () {

                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length !== 1) {
                        return layer.msg('请选择一张优惠券');
                    }

                    if (checkData[0].State_Sel != 1) {
                        return layer.msg('未激活的优惠券不能发送');
                    }

                    admin.popup({
                        title: '发送优惠券根据等级'
                        , skin: 'layui-layer-lan'
                        , area: ['30%', '70%']
                        , zIndex: 1003
                        , id: 'LAY-popup-content-add'
                        , success: function (layero, index) {
                            view(this.id).render('/shop/shop_coupon/SendCoupon', checkData[0]).done(function () {

                            })
                        }
                    });
                },

                sendCouponName: function () {

                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length !== 1) {
                        return layer.msg('请选择一张优惠券');
                    }

                    if (checkData[0].State_Sel != 1) {
                        return layer.msg('未激活的优惠券不能发送');
                    }

                    admin.popup({
                        title: '发送优惠券根据名称'
                        , skin: 'layui-layer-lan'
                        , area: ['30%', '30%']
                        , zIndex: 1003
                        , id: 'LAY-popup-content-add'
                        , success: function (layero, index) {
                            view(this.id).render('/shop/shop_coupon/SendCouponName', checkData[0]).done(function () {

                            })
                        }
                    });
                },

            };

            $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    }

</script>